﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


namespace chamaLinxDtef
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        LinxDtefTelas.cDtef frm = new LinxDtefTelas.cDtef();
        DataTable dtREsultadoTEF = new DataTable();
     
        public int iRes;

        private void button1_Click(object sender, EventArgs e)
        {
            //string sResposta;
          
            ////iRes = frm.MostraForm("VendaCredito", "EntraValor", out sResposta);
            frm.Load();
              try
            {

                if (this.cbCredito.Checked)
                {
                    iRes = frm.VendaCredito(this.tbValor.Text, this.tbCupom.Text, this.tbNsu.Text);
                }
                if (this.cbDebito.Checked)
                {
                    iRes = frm.VendaDebito(this.tbValor.Text, this.tbCupom.Text, this.tbNsu.Text);
                }
            }
            catch (Exception d)
            {
                throw new Exception("VendaCredito.gCredito" + d.Message, d);
            }

            string teste;
            teste = frm.ObtemLog(false);
            StringBuilder sb = new StringBuilder();
            using (StreamWriter outfile = new StreamWriter(@"L:\LinxPOS\Log\ObtemLogEstendidoDPOSSRV.txt"))
            {
                sb.Append(teste);
                outfile.Write(sb.ToString());
            }

            /**/
            //ResultadoTef
            sb.Clear();
            sb.Append(teste.Substring(160, 6));
            frm.ConfirmaCredito(teste.Substring(160, 6));
            frm.FinalizarTransacao();
            if (iRes == 0)
            {
                MessageBox.Show("Venda concluida com sucesso");
            }
            else
            {
                MessageBox.Show("Não foi possivel concluir a venda ! Verifique");
            }
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string regasmPath = RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";

            // Get the location of our DLL.
            string componentPath = "L:\\Telas CallBack\\LinxDtef\\bin\\x86\\Debug\\LinxDtef.dll";

            // Execute regasm.
            Process p = new Process();
            p.StartInfo.FileName = regasmPath;
            p.StartInfo.Arguments = "\"" + componentPath + "\" /tlb /codebase";
            p.StartInfo.Verb = "runas";          // To run as administrator.
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.Start();
            p.WaitForExit();
            MessageBox.Show("Dll Registrada com sucesso !");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string teste;
            frm.Load();
            teste = frm.ObtemLog(false);
            StringBuilder sb = new StringBuilder();
            using (StreamWriter outfile = new StreamWriter(@"C:\Users\diego.moreno\Desktop\LogDPOS\LOG.txt"))
            {
                sb.Append(teste);
                outfile.Write(sb.ToString());
            }
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string teste;
            frm.Load();
            teste = frm.ObtemLog(false);
            StringBuilder sb = new StringBuilder();
            using (StreamWriter outfile = new StreamWriter(@"C:\Users\diego.moreno\Desktop\LogDPOS\LOG.txt"))
            {
                sb.Append(teste);
                outfile.Write(sb.ToString());
            }
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int iResultado;
            string teste;
            frm.Load();
            teste = frm.ObtemLog(false);
            StringBuilder sb = new StringBuilder();
            using (StreamWriter outfile = new StreamWriter(@"L:\LinxPOS\Log\ObtemLogEstendidoDPOSSRV.txt"))
            {
                sb.Append(teste);
                outfile.Write(sb.ToString());
            }

            /**/
            //ResultadoTef
            sb.Clear();
            sb.Append(teste.Substring(160, 6));
            //frm.ConfirmaCredito(sb);
            iResultado = frm.CancelarTransacao(this.tbNsu.Text);
            frm.ConfirmaCredito(teste.Substring(160, 6));
            frm.FinalizarTransacao();
            if (iResultado == 0)
            {
                MessageBox.Show("Venda cancelada com sucesso");
            }
            else
            {
                MessageBox.Show("Atenção !. A venda não foi cancelada verifique seu idiota !");
            }
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {

           

        }
    }
}
